package cn.myzf.module.controller;

import cn.myzf.module.annotation.NettyController;
import cn.myzf.module.annotation.NettyRequestMapping;
import cn.myzf.module.annotation.NettyResponsebody;
import cn.myzf.module.instance.InstanceConfig;
import cn.myzf.module.instance.InstanceInfo;
import cn.myzf.module.register.Register;
import cn.myzf.module.util.ResponseInfo;


/**
 * @package cn.myzf.module.controller
 * @Date Created in 2020/3/7 21:14
 * @Author myzf
 */

@NettyController
public class AppController {

    public static final int DEFULT_TIME_LIMIT=90;


    private Register register;

    private InstanceConfig instanceConfig;

    public AppController (Register register,InstanceConfig instanceConfig){
        this.register = register;
        this.instanceConfig = instanceConfig;
    }



    public boolean isNull(String str) {
        return str == null || str.isEmpty();
    }

    //测试链接http://localhost:8081/register.do?ipAddr=127.0.0.1&hostName=xxx&instanceId=clinet-001&instanceName=client&timeLimit=4000&port=8080&token=myzf


    @NettyRequestMapping("/register.do")
    @NettyResponsebody
    public ResponseInfo register(InstanceInfo instanceInfo, String isSync) {
        if (isNull(instanceInfo.getHostName())) {
            return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS,"HostName 不能为空");
        } else if (isNull(instanceInfo.getInstanceId())) {
            return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS,"InstanceId 不能为空");

        } else if (isNull(instanceInfo.getInstanceName())) {

            return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS,"InstanceName 不能为空");

        } else if (isNull(instanceInfo.getPort())) {
            return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS,"Port 不能为空");

        } else if (isNull(instanceInfo.getIpAddr())) {
            return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS,"IpAddr 不能为空");

        }
        int timeLimit = DEFULT_TIME_LIMIT;
        if (instanceInfo.getTimeLimit()>0) {
            timeLimit = instanceInfo.getTimeLimit();
        }
        return ResponseInfo.build(ResponseInfo.CODE_OK,instanceInfo.getHostName()+"注册成功");
    }


    @NettyRequestMapping("/renew.do")

    public ResponseInfo renew(String instanceName,String instanceId,String isSync){
        if (isNull(instanceName)){
            return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS,"instanceName 不能为空");

        }else if(isNull(instanceId)){
            return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS,"InstanceId 不能为空");

        }
        register.renew(instanceName,instanceId,isSync==null?false:isSync.equals("true"));
        return ResponseInfo.build(ResponseInfo.CODE_OK,"IpAddr 不能为空");
    }

    @NettyRequestMapping("/cancel.do")
    public ResponseInfo cancel(String instanceName,String instanceId,String isSync){
        if (isNull(instanceName)){
            return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS,"instanceName 不能为空");

        }else if(isNull(instanceId)){
            return ResponseInfo.build(ResponseInfo.CODE_INVILID_PARAMS,"instanceId 不能为空");

        }
        register.cancel(instanceName,instanceId,isSync==null?false:isSync.equals("true"));
        return ResponseInfo.build(ResponseInfo.CODE_OK,"IpAddr 不能为空");
    }



}
